#pragma once
#include <iostream>
#include <string>
#include <unistd.h>
#include "task.hpp"
class Channel
{
public:
    Channel(int wfd, int who)
        : _wfd(wfd), _who(who)
    {
        _name = "channels-" + std::to_string(wfd) + "-" + std::to_string(who);
    }
    std::string Name()
    {
        return _name;
    }
    void Send(int cmd)
    {

        ::write(_wfd, &cmd, sizeof(cmd));
    }
    int id()
    {
        return _who;
    }
    int wfd()
    {
        return _wfd;
    }
    void Close()
    {
        ::close(_wfd);
    }
    ~Channel()
    {
    }

private:
    int _wfd; // 子进程管道的写文件符
    std::string _name;
    int _who; // 子进程pid
};
void Works()
{
    // 子进程工作
    while (true)
    {
        int cmd;
        int n = ::read(0, &cmd, sizeof(cmd));
        if (n == sizeof(cmd))
        {
            tm.Excute(cmd);
        }
        else if (n == 0)
        {
            break;
        }
        sleep(1);
    }
}